package com.shop2cn.iapi.sqoptrade.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.req.UpdateOrderProductSkuReq;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.UpdateOrderProductSkuCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.sharding.db.ext.model.OrderDetailExtPo;
import com.ymttest.database.sharding.db.sharding.model.OrderBizLogPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sharding.db.sqlwapper.OrderWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

public class Ts_SqUpdateOrderProductSku {

	private static UpdateOrderProductSkuReq updateOrderProductSkuReq;
	private static UpdateOrderProductSkuCall updateOrderProductSkuCall;
	
	private static OrderWapper orderWapper = new OrderWapper();
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商品sku");
	}

	@Before
	public void caseUp() {
		updateOrderProductSkuReq = new UpdateOrderProductSkuReq();
		updateOrderProductSkuCall = new UpdateOrderProductSkuCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateOrderProductSkuCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public static void verify(long orderId, String catalogId) {
		
		Logger.comment("验证order_detail_ext");

		Long orderDetailId = orderShardingWapper
				.selectOrderDetailbyOrderIdAndCatalogId(orderId, catalogId)
				.getOrderDetailId();
		OrderDetailExtPo orderDetailExtPo = orderWapper
				.selectOrderDetailExtByOrderId(orderDetailId);

		Logger.verifyEquals(updateOrderProductSkuReq.getSku(),
				orderDetailExtPo.getSku(), "验证数据库sku");

		Logger.comment("验证order_biz_log");

		OrderBizLogPo orderBizLogPo = orderShardingWapper
				.selectOrderBizLogByOrderId(orderId);

		Logger.verifyEquals(orderId, orderBizLogPo.getOrderId(), "验证数据库OrderId");
		Logger.verifyEquals(updateOrderProductSkuReq.getOperatorId(),
				orderBizLogPo.getOperator(), "验证数据库Operator");
		Logger.verifyEquals("ADD_SELLERMEMO", orderBizLogPo.getBizType(),
				"验证数据库BizType");
		Logger.verifyEquals(String.valueOf(orderId), orderBizLogPo.getBizNo(),
				"验证数据库BizNo");
		Logger.verifyEquals(String.format("更新订单商品catalogId:%s sku:%s",
				catalogId, updateOrderProductSkuReq.getSku()), orderBizLogPo
				.getContent(), "验证数据库Content");
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateOrderProductSku_001() {
		Logger.start(true, "创建一个订单，店主更新商品sku成功");
		try {
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, true);
			
			TradingServiceResp resp = SqTradingCallService.placeOrder(
					productBean, autotest_goodsOwnerId);
			
			Thread.sleep(3000);

			long orderId = resp.getOrderList().get(0).getOrderId();
			String catalogId = productBean.getCatalogList().get(0);

			updateOrderProductSkuReq.setOrderId(orderId);
			updateOrderProductSkuReq.setCatalogId(catalogId);
			updateOrderProductSkuReq.setOperatorId(productBean.getSellerId());
			updateOrderProductSkuReq.setSku(TestDataManager.getRandomNum(8));
			updateOrderProductSkuReq.setSellerId(productBean.getSellerId());

			updateOrderProductSkuCall.setData(updateOrderProductSkuReq);
			updateOrderProductSkuCall.callService();

			verify(orderId, catalogId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateOrderProductSku_002() {
		Logger.start(true, "创建一个订单，货头更新sku成功");
		try {
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder(
					productBean, autotest_goodsOwnerId);

			Thread.sleep(3000);
			
			long orderId = resp.getOrderList().get(0).getOrderId();
			String catalogId = productBean.getCatalogList().get(0);

			updateOrderProductSkuReq.setOrderId(orderId);
			updateOrderProductSkuReq.setCatalogId(catalogId);
			updateOrderProductSkuReq.setOperatorId(productBean.getSellerId());
			updateOrderProductSkuReq.setSku(TestDataManager.getRandomNum(8));
			updateOrderProductSkuReq.setSellerId(productBean.getGoodsOwnerId());

			updateOrderProductSkuCall.setData(updateOrderProductSkuReq);
			updateOrderProductSkuCall.callService();
			
			verify(orderId, catalogId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateOrderProductSku_003() {
		Logger.start(true, "创建一个订单，中间商更新sku失败");
		try {

			updateOrderProductSkuReq.setOrderId(1520053760L);
			updateOrderProductSkuReq.setCatalogId("c7596121");
			updateOrderProductSkuReq.setOperatorId(501191433L);
			updateOrderProductSkuReq.setSku(TestDataManager.getRandomNum(8));
			updateOrderProductSkuReq.setSellerId(501191434L);

			updateOrderProductSkuCall.setData(updateOrderProductSkuReq);
			updateOrderProductSkuCall.callService();
			
			Logger.verifyEquals(false, updateOrderProductSkuCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(198,
					updateOrderProductSkuCall.getCode(), "验证errorCode");
			Logger.verifyEquals(true, updateOrderProductSkuCall.getMsg()
					.contains("当前订单不允许更新商品sku操作"), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateOrderProductSku_006() {
		Logger.start(true, "一个不存在的订单，店主更新sku失败");
		try {

			updateOrderProductSkuReq.setOrderId(1L);
			updateOrderProductSkuReq.setCatalogId("c7596121");
			updateOrderProductSkuReq.setOperatorId(501191433L);
			updateOrderProductSkuReq.setSku(TestDataManager.getRandomNum(8));
			updateOrderProductSkuReq.setSellerId(501191434L);

			updateOrderProductSkuCall.setData(updateOrderProductSkuReq);
			updateOrderProductSkuCall.callService();

			Logger.verifyEquals(false, updateOrderProductSkuCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(198,
					updateOrderProductSkuCall.getCode(), "验证errorCode");
			Logger.verifyEquals(true, updateOrderProductSkuCall.getMsg()
					.contains("订单不存在"), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateOrderProductSku_007() {
		Logger.start(true, "创建一个订单，店主更新sku，货头再次更新备注成功");
		try {
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder(
					productBean, autotest_goodsOwnerId);
			
			Thread.sleep(3000);

			long orderId = resp.getOrderList().get(0).getOrderId();
			String catalogId = productBean.getCatalogList().get(0);

			// 第一次更新备注
			updateOrderProductSkuReq.setOrderId(orderId);
			updateOrderProductSkuReq.setCatalogId(catalogId);
			updateOrderProductSkuReq.setOperatorId(productBean.getSellerId());
			updateOrderProductSkuReq.setSku(TestDataManager.getRandomNum(8));
			updateOrderProductSkuReq.setSellerId(productBean.getSellerId());
			
			updateOrderProductSkuCall.setData(updateOrderProductSkuReq);
			updateOrderProductSkuCall.callService();

			// 第二次更新备注
			updateOrderProductSkuReq.setOrderId(orderId);
			updateOrderProductSkuReq.setCatalogId(catalogId);
			updateOrderProductSkuReq.setOperatorId(productBean.getSellerId());
			updateOrderProductSkuReq.setSku(TestDataManager.getRandomNum(8));
			updateOrderProductSkuReq.setSellerId(productBean.getGoodsOwnerId());

			updateOrderProductSkuCall.setData(updateOrderProductSkuReq);
			updateOrderProductSkuCall.callService();

			verify(orderId, catalogId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
